package com.java.al.swing;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.JTextField;
 
/**
* 重置按钮监听器
* @author hyc
* @create 2009-8-8
*/
public class SuperResetButtonAction implements ActionListener {
       
        private Container container;
       
        public SuperResetButtonAction(Container container) {
                this.container = container;
        }
       
        /**
         * 清空容器内容
         * @param parent
         */
        private void clear(Container parent) {
                for(Component component: parent.getComponents()){
                        if(component instanceof Container) {
                                clear((Container) component);
                        }
                        if(component instanceof JTextField) {
                                ((JTextField) component).setText("");
                        }
                        if(component instanceof JTextArea) {
                                ((JTextArea) component).setText("");
                        }
                        if(component instanceof JComboBox) {
                                if(((JComboBox) component).getItemCount()>0) {
                                        ((JComboBox) component).setSelectedIndex(0);
                                }
                        }
                        if(component instanceof JList) {
                                ((JList)component).clearSelection();
                        }
                }
        }
 
        public void actionPerformed(ActionEvent arg0) {
                clear(container);
        }
 
}